package com.hospital.service.impl;

import com.hospital.domain.pojo.Bed;
import com.hospital.domain.utils.Result;
import com.hospital.mapper.BedMapper;
import com.hospital.service.BedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BedServiceImpl implements BedService {

    @Autowired
    private BedMapper bedMapper;

    @Override
    public Result<List<Bed>> getBedsByWard(Integer wId) {
        try {
            List<Bed> beds = bedMapper.selectByWard(wId);
            return Result.ok(beds);
        } catch (Exception e) {
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    @Override
    public Result<List<Bed>> getAvailableBeds() {
        try {
            List<Bed> beds = bedMapper.selectAvailable();
            return Result.ok(beds);
        } catch (Exception e) {
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    @Override
    public Result<String> updateBedState(Integer bId, Integer state) {
        try {
            int result = bedMapper.updateBedState(bId, state);
            if (result > 0) {
                return Result.ok("床位状态更新成功");
            } else {
                return Result.error("更新失败");
            }
        } catch (Exception e) {
            return Result.error("系统异常：" + e.getMessage());
        }
    }

    @Override
    public Result<Bed> getBedById(Integer bId) {
        try {
            Bed bed = bedMapper.selectById(bId);
            if (bed != null) {
                return Result.ok(bed);
            } else {
                return Result.error("床位不存在");
            }
        } catch (Exception e) {
            return Result.error("查询失败：" + e.getMessage());
        }
    }
}
